package com.ctsi.framework.common.response;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: zhangjw
 * @Date: 2018/9/16 18:38
 * @param <E> 真正的实体对象
 * @Description: 统一响应模型，切记只可以用于app rest 的响应
 */
public class UnifiedResponse<E> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 是否成功标记
     */
    private final boolean success;

    /**
     * 错误代码
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private final String code;
    /**
     * 错误信息
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private final Object error;
    /**
     * 承载数据
     */
    private final E data;

    /**
     * 时间戳
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private final Long timestamp;


    private UnifiedResponse(boolean success, String code, Object error, E data) {
        super();
        this.success = success;
        this.code = code;
        this.error = error;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    public boolean isSuccess() {
        return this.success;
    }

    public String getCode() {
        return this.code;
    }

    public Object getError() {
        return this.error;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public E getData() {
        return this.data;
    }

    /**
     *
     * @param data
     * @param <E>
     * @return
     */
    public static <E> UnifiedResponse<E> ok(E data) {
        return new UnifiedResponse<>(true,null,null,data);
    }

    /**
     *
     * @return
     */
    public static  UnifiedResponse<Void> ok() {
        return new UnifiedResponse<>(true,null,null,null);
    }

    /**
     *
     * @param code
     * @param error
     * @return
     */
    public static UnifiedResponse<Void> error(String code,Object error){
        return new UnifiedResponse(false,code,error,null);
    }

    public static UnifiedResponse<Void> error(String code,String error){
        return new UnifiedResponse(false,code,error,null);
    }

    public static UnifiedResponse<Void> error(String code,String ... error){
        return new UnifiedResponse(false,code, Stream.of(error).collect(Collectors.joining(",")),null);
    }

    /**
     * @param code
     * @param error
     * @param data
     * @param <E>
     * @return
     */
    public static <E> UnifiedResponse<E> error(String code,Object error,E data){
        return new UnifiedResponse(false,code,error,data);
    }

    /**
     *
     * @param code
     * @param error
     * @param data
     * @param <E>
     * @return
     */
    public static <E> UnifiedResponse<E> error(String code,String error,E data){
        return new UnifiedResponse(false,code,error,data);
    }

    /**
     *
     * @param code
     * @param data
     * @param error
     * @param <E>
     * @return
     */
    public static <E> UnifiedResponse<E> error(String code,E data,String ... error){
        return new UnifiedResponse(false,code,Stream.of(error).collect(Collectors.joining(";")),data);
    }
}
